import { uuid, eachTreeData } from "../utils/core";
const DEFAULT_REQUEST = {
  pageName: "page",
  limitName: "limit",
  sortName: "sort",
  orderName: "order"
};
const DEFAULT_RESPONSE = {
  statusName: "code",
  statusCode: 0,
  msgName: "msg",
  dataName: "data",
  countName: "count"
};
const DEFAULT_ASCEND_VALUE = "asc";
const DEFAULT_DESCEND_VALUE = "desc";
const HIDE_SETTING_TYPES = ["selection", "index", "expand"];
function getFieldValue(data, field) {
  if (typeof field === "function") {
    return field(data);
  }
  if (field) {
    let value = data;
    field.split(".").forEach((f) => {
      value = value ? value[f] : null;
    });
    return value;
  }
}
function getOrderItems(sorter) {
  var _a, _b;
  const orders = [];
  if (!sorter || !sorter.order) {
    return orders;
  }
  const descend = sorter.order === "descending";
  const sortMethod = (_a = sorter.column) == null ? void 0 : _a.sortMethod;
  const sortBy = (_b = sorter.column) == null ? void 0 : _b.sortBy;
  if (Array.isArray(sortBy) && sortBy.length) {
    sortBy.forEach((field) => {
      if (typeof field === "string" && field.length) {
        orders.push({ field, descend, sortMethod });
      }
    });
  } else {
    const field = typeof sortBy === "string" && sortBy ? sortBy : sorter.prop;
    if (field) {
      orders.push({ field, descend, sortMethod });
    }
  }
  return orders;
}
function reloadData(datasource, sorter, page, limit, needPage) {
  const data = [...datasource];
  const total = data.length;
  getOrderItems(sorter).forEach((item) => {
    data.sort((a, b) => {
      if (typeof item.sortMethod === "function") {
        const r = item.sortMethod(a, b);
        return item.descend ? -r : r;
      } else {
        const av = a[item.field];
        const bv = b[item.field];
        if (av == bv) {
          return 0;
        } else if (item.descend) {
          return av < bv ? 1 : -1;
        } else {
          return av < bv ? -1 : 1;
        }
      }
    });
  });
  if (needPage && page && limit) {
    const maxPage = Math.ceil(total / limit);
    if (maxPage && page > maxPage) {
      page = maxPage;
    }
    const start = (page - 1) * limit;
    const end = start + limit;
    const list = data.slice(start, end > total ? total : end);
    return { page, data: list, total };
  } else {
    return { page: 1, data, total };
  }
}
function getInitColumns(columns) {
  const data = [];
  columns.forEach((d) => {
    var _a;
    if (d.show !== false) {
      const id = generateColId();
      if ((_a = d.children) == null ? void 0 : _a.length) {
        const children = getInitColumns(d.children);
        if (children.length) {
          data.push(Object.assign({}, d, { children, id }));
        }
      } else {
        data.push(Object.assign({}, d, { id }));
      }
    }
  });
  return data;
}
function generateColId() {
  return uuid(8);
}
function getColKey(column) {
  return column ? column.columnKey || column.prop : void 0;
}
function getCheckedColumns(columns, cols, checkedIds, sortable) {
  const data = [];
  if (columns) {
    columns.forEach((d) => {
      const key = getColKey(d);
      const id = generateColId();
      if (cols.some((c) => c.id === key) && !checkedIds.includes(key)) {
        return;
      }
      if (d.children) {
        const children = getCheckedColumns(d.children, cols, checkedIds, sortable);
        if (children.length) {
          data.push(Object.assign({}, d, { children, id }));
        }
      } else {
        data.push(Object.assign({}, d, { id }));
      }
    });
  }
  if (sortable && data.length) {
    data.sort((a, b) => {
      var _a, _b;
      const aId = getColKey(a);
      const bId = getColKey(b);
      let ai = cols.findIndex((c) => c.id === aId);
      let bi = cols.findIndex((c) => c.id === bId);
      if (ai === -1 && ((_a = a.children) == null ? void 0 : _a.length)) {
        const id = getColKey(a.children[0]);
        ai = cols.findIndex((c) => c.id === id);
      }
      if (bi === -1 && ((_b = b.children) == null ? void 0 : _b.length)) {
        const id = getColKey(b.children[0]);
        bi = cols.findIndex((c) => c.id === id);
      }
      if (a.fixed === true || a.fixed === "left") {
        ai -= cols.length;
      } else if (a.fixed === "right" || a.hideInSetting && !a.fixed) {
        ai += cols.length;
      }
      if (b.fixed === true || b.fixed === "left") {
        bi -= cols.length;
      } else if (b.fixed === "right" || b.hideInSetting && !b.fixed) {
        bi += cols.length;
      }
      return ai - bi;
    });
  }
  return data;
}
function getSettingCols(columns, untitledText) {
  const data = [];
  if (columns) {
    eachTreeData(columns, (d) => {
      var _a;
      const id = getColKey(d);
      if (id && !((_a = d.children) == null ? void 0 : _a.length) && !d.hideInSetting && !HIDE_SETTING_TYPES.includes(d.type)) {
        data.push({
          id,
          prop: d.prop,
          columnKey: d.columnKey,
          label: d.label || untitledText,
          checked: d.show !== false
        });
      }
    });
  }
  const checkedIds = data.filter((d) => d.checked).map((d) => d.id);
  return {
    data,
    checkedIds,
    isAllChecked: data.length > 0 && data.length === checkedIds.length,
    isIndeterminate: data.length !== checkedIds.length
  };
}
export { DEFAULT_ASCEND_VALUE, DEFAULT_DESCEND_VALUE, DEFAULT_REQUEST, DEFAULT_RESPONSE, HIDE_SETTING_TYPES, generateColId, getCheckedColumns, getColKey, getFieldValue, getInitColumns, getOrderItems, getSettingCols, reloadData };
